﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_FinMov_FinMov001d : AdminBasePage
{
    decimal shipID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }

        if (!IsPostBack)
        {
            FinShipping fsp = new FinShipping();
            fsp.LoadByPrimaryKey(shipID);
            txtType.Text = getDescByCode(fsp.TransferType);
            txtFrom.Text = getThirdName(fsp.FromDestination.Value.ToString());
            txtTo.Text = getThirdName(fsp.ToDestination.Value.ToString());
            txtComments.Text = fsp.Comments;
            GridDataBind();
        }


    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        try
        {
            if (drpManager.SelectedValue.Length == 0)
            {
                lbMessage.Text = "请选择CF/ACC主管!";
                return;
            }

            FinShipping fsp = new FinShipping();
            fsp.LoadByPrimaryKey(shipID);
            fsp.ShipManagerMail = drpManager.SelectedValue;
            fsp.UpdatedDttm = DateTime.Now;
            fsp.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
            if (fsp.TransferType == ConstantValue.TransferType.YearTransfer)
            {
                fsp.ShippingStatus = ConstantValue.ShippingStatusCode.NeedReply;//只有年度转移的才要去判断
            }
            else
            {
                fsp.ShippingStatus = ConstantValue.ShippingStatusCode.Pending;
            }
            fsp.Save();

           

            TSCodeTable codeTable = new TSCodeTable();
            codeTable.Query.Where(codeTable.Query.Code == fsp.TransferType);
            codeTable.Query.es.Top = 1;
            codeTable.Query.Load();

            BFThirdUnit btu = new BFThirdUnit();
            btu.LoadByPrimaryKey(fsp.FromDestination.Value);

            //只有外部转移才发送邮件
            if (fsp.TransferType == "ZYLX003")
            {
                TriggerMail(fsp.CreatedDttm.Value.ToString("yyMMdd") + fsp.ShipID.ToString(), codeTable.Value1, btu.Name, fsp.CreatedDttm.Value.Year.ToString());
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

    //TO DO
    void TriggerMail(string finMoveNo,string transferType, string from,string year)
    {
        List<string> listTo = new List<string>();
        List<string> listCC = new List<string>();

        string subject = string.Format(yncSettings.Mail.FinMove.MailSubject, finMoveNo);
        string content = yncSettings.Mail.FinMove.MailContent;
               
        string loginUrl = WebHelper.RootUrl() + "/Admin/AdminLogin.aspx";

        TSUserInfoCollection users = new TSUserInfoCollection();
        users.Query.Where(users.Query.ThirdUnitID == LoginUserInfo.CurrentUser.ThirdUnitID);
        users.Query.Load();

        if (users.HasData)
        {
            foreach (var item in users)
            {
                if (!item.Email.Equals(drpManager.SelectedValue))
                {
                    listCC.Add(item.Email);
                }
            }
        }

        //Send mail to all user(same company)
        SendMail.SendHtml(new string[] { drpManager.SelectedValue }, listCC.ToArray(), subject, string.Format(content, finMoveNo, from, loginUrl));
    }

    void GridDataBind()
    {

        VBoxReceiptsQuery q1 = new VBoxReceiptsQuery();
        FinShippingBoxesQuery q2 = new FinShippingBoxesQuery();

        q1.SelectAll();
        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.Where(q2.ShipID == shipID);
        var dataBoxs= q1.LoadDataTable();
        rpt.DataSource = dataBoxs;
        rpt.DataBind();

        if (!WebHelper.CheckIsNotEmpty(dataBoxs))
        {
            btnSave.Enabled = false;
            lbMessage.Text = "请添加您要转移的凭证!";
        }

        TSUserInfoQuery tq1 = new TSUserInfoQuery("tq1");
        TSUserRolesQuery tq2 = new TSUserRolesQuery("tq2");
        tq1.InnerJoin(tq2).On(tq1.UserID == tq2.UserID);
        tq1.Where(tq2.RoleID == 3);
        tq1.Select(tq1.Email.As("Value"), (tq1.UserName + " - "+ tq1.Email).As("Text"));
        var dataTable = tq1.LoadDataTable();
        drpManager.DataSource = dataTable;
        drpManager.DataTextField = "Text";
        drpManager.DataValueField = "Value";
        drpManager.DataBind();



    }


    private string getDescByCode(string code)
    {
        TSCodeTableCollection tSCodeTableCollection = new TSCodeTableCollection();
        tSCodeTableCollection.Query.Where(tSCodeTableCollection.Query.Code==code);
        tSCodeTableCollection.Query.Load();
        if (tSCodeTableCollection.Count==1)
        {
            return tSCodeTableCollection[0].CodeDesc;
        }
        else
        {
            return "";
        }
    }

    private string getThirdName(string Id)
    {
        BFThirdUnit bFThirdUnit = new BFThirdUnit();
        bFThirdUnit.LoadByPrimaryKey(decimal.Parse(Id));
        if (bFThirdUnit.es.HasData)
        {
            return bFThirdUnit.Name;
        }
        else
        {
            return "";
        }
    }
}